﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace storeSMS
{
    public partial class frmLoadCustomer : Form
    {
        storeSMS.BLL.storeinfo sm = new BLL.storeinfo();

        public frmLoadCustomer()
        {
            InitializeComponent();
        }

        //导入客户资料
        private void btn_Load_Click(object sender, EventArgs e)
        {

            OpenFileDialog fileDialog = new OpenFileDialog();            
            fileDialog.Multiselect = true;             
            fileDialog.Title = "请选择文件";             
            fileDialog.Filter = "所有文件(*.*)|*.*";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                string file = fileDialog.FileName;
                //MessageBox.Show("已选择文件:" + file, "选择文件提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                this.txt_path.Text = file;

                DataSet ds = new DataSet();
                string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + file + ";" + "Extended Properties='Excel 8.0; HDR=Yes; IMEX=1';";
                //string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + file + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
                //string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + file + ";Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'";
                OleDbConnection conn = new OleDbConnection(strConn);
                conn.Open();
                string strExcel = "";
                OleDbDataAdapter myCommand = null;
                strExcel = string.Format("select * from [{0}$]", "Sheet1"); //Bicycle为excel中工作薄
                myCommand = new OleDbDataAdapter(strExcel, strConn);
                myCommand.Fill(ds, "yuegongjihua");

                if (ds.Tables[0].Rows.Count > 3)
                {
                   
                    for (int i = 2; i < ds.Tables[0].Rows.Count; i++)
                    {
                        DataRow r = ds.Tables[0].Rows[i];

                        string gys = r[2].ToString();
                        string project = r[3].ToString();
                        string projectbh = r[4].ToString();
                        string cgddh = r[11].ToString();
                        string phone = r[13].ToString();
                        string qr = r[6].ToString();
                        string gh  = r[7].ToString();
                        string nums = r[8].ToString();

                        Maticsoft.Model.storeinfo s = new Maticsoft.Model.storeinfo();

                        s.gys = gys;
                        if (!string.IsNullOrEmpty(gh))
                        {
                            s.gh = DateTime.Parse(gh);
                        }
                        else
                        {
                            s.gh = DateTime.Parse("1900-01-01");
                        }
                        if (!string.IsNullOrEmpty(qr))
                        {
                            s.qr = DateTime.Parse(qr);
                        }
                        else
                        {
                            s.qr = DateTime.Parse("1900-01-01");
                        }
                        s.nums = nums;
                        s.phone = phone;
                        s.project = project;
                        s.projectbh = projectbh;
                        s.cgddh = cgddh;

                        sm.Add(s);
                       
                    }
                }

            }

            DataBind();
        }

        private void frmLoadCustomer_Load(object sender, EventArgs e)
        {
            DataBind();
            
        }

        private void DataBind()
        {
            DataSet ds = sm.GetList("");
            this.dgvsendinfo.AutoGenerateColumns = false;
            this.dgvsendinfo.DataSource = ds.Tables[0];
            
        }
    }
}
